package movie.controller;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Consumer;
import movie.service.FilmService;
import movie.service.LoginService;

@Controller
public class HomeController {
	@Autowired
	private FilmService filmService;
	@Autowired
	private LoginService loginService;
	
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String gethome(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		if(session.getAttribute("cur_user") != null) {
			Consumer consumer1 = (Consumer)session.getAttribute("cur_user");
			
			Calendar calendar = Calendar.getInstance();
			Date curTime = calendar.getTime();
			Date termTime = consumer1.getMemberDate();
			
			if(curTime.after(termTime)) {
				Calendar c = Calendar.getInstance();
				c.set(2099, 1, 1);
				termTime = c.getTime();
				consumer1.setMemberId(1);
				consumer1.setMemberDate(termTime);
				loginService.update_member(consumer1);
			}
			
		}
		model.addAttribute("reying_films", filmService.find_home_reying());
		model.addAttribute("willshow_films", filmService.find_home_willshow());
		model.addAttribute("showed_films", filmService.find_home_showed());
		model.addAttribute("piaofang_films", filmService.find_home_piaofang());
		model.addAttribute("wantsee_films", filmService.find_home_wantsee());
		return "forward:home.jsp";
	}
	
}
